package com.weigee.ktv.view.fragment

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.linde.library.activities.MyFragmentActivity
import com.linde.library.util.DecimalPointUtil
import com.linde.library.util.DisplayUtil
import com.linde.library.util.ToastUtil
import com.weigee.ktv.R
import com.weigee.ktv.presenter.RechargeReturnPresenter
import com.weigee.ktv.view.activity.PayingActivity
import com.weigee.ktv.view.dialog.LoadingDialog
import com.weigee.ktv.view.interfaces.RechargeReturnView
import kotlinx.android.synthetic.main.frag_recharge_return.*
import kotlinx.android.synthetic.main.include_status_bar_height.*
import kotlinx.android.synthetic.main.include_title_bar.*

/**
 * Created by linde on 2017/6/7.
 * LinDe
 */
class RechargeReturnFragment : BaseFragment(), RechargeReturnView, TextWatcher
{

    var presenter: RechargeReturnPresenter? = null
    var loadingDialog: LoadingDialog? = null
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        presenter = RechargeReturnPresenter(this)
        presenter?.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        return inflater?.inflate(R.layout.frag_recharge_return, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        DisplayUtil.setStatusBarHeight(linear_status_bar_height)
        // 返回
        my_title_bar.leftIconClickBack = activity
        // 标题
        my_title_bar.title = "充值返利"
        // 手动输入充值金额监听
        et_input_recharge.addTextChangedListener(this)
        // 充值返利列表
        gv_recharge_return_list.adapter = presenter?.adapter
        // 提交充值
        tv_recharge_commit.setOnClickListener {
            MyFragmentActivity.start(PayingActivity::class.java, activity, PayingFragment::class.java)
        }
        // 用户协议
        linear_user_rules.setOnClickListener { v -> ToastUtil.show(v, "用户协议") }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?)
    {
        super.onActivityCreated(savedInstanceState)
        presenter?.onActivityCreated(savedInstanceState)
    }

    override fun afterTextChanged(s: Editable?)
    {
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int)
    {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int)
    {
    }

    override fun taskPreview()
    {
        loadingDialog?.dismiss()
        loadingDialog = LoadingDialog()
        loadingDialog?.show(childFragmentManager, javaClass.name)
    }

    override fun taskCompletely()
    {
        loadingDialog?.dismiss()
    }

    override fun setBalance(balance: Float)
    {
        tv_balance.text = DecimalPointUtil.point(2, balance)
    }
}